package com.carrental.integration.controller;

import com.carrental.common.result.Result;
import com.carrental.integration.service.MapService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 地图服务控制器
 * 提供地图相关的API接口
 */
@Tag(name = "地图服务接口", description = "提供地图相关的API接口")
@RestController
@RequestMapping("/map")
public class MapController {

    @Resource
    private MapService mapService;

    /**
     * 地理编码API，将地址信息转换为经纬度坐标
     *
     * @param address 地址信息
     * @return 包含经纬度坐标的结果
     */
    @Operation(summary = "地理编码-地址转坐标", description = "将地址信息转换为经纬度坐标")
    @GetMapping("/geocoding")
    public Result<Map<String, Object>> geocoding(
            @Parameter(description = "地址信息", required = true) @RequestParam String address) {
        Map<String, Object> result = mapService.geocoding(address);
        if (result.isEmpty()) {
            return Result.failed("地理编码失败");
        }
        return Result.success(result);
    }

    /**
     * 逆地理编码API，将经纬度坐标转换为地址信息
     *
     * @param latitude 纬度
     * @param longitude 经度
     * @return 包含地址信息的结果
     */
    @Operation(summary = "逆地理编码-坐标转地址", description = "将经纬度坐标转换为地址信息")
    @GetMapping("/reverse-geocoding")
    public Result<Map<String, Object>> reverseGeocoding(
            @Parameter(description = "纬度", required = true) @RequestParam double latitude,
            @Parameter(description = "经度", required = true) @RequestParam double longitude) {
        Map<String, Object> result = mapService.reverseGeocoding(latitude, longitude);
        if (result.isEmpty()) {
            return Result.failed("逆地理编码失败");
        }
        return Result.success(result);
    }

    /**
     * 路径规划API，计算两点间的驾车路线
     *
     * @param originLat 起点纬度
     * @param originLng 起点经度
     * @param destLat 终点纬度
     * @param destLng 终点经度
     * @return 包含路径规划结果的结果
     */
    @Operation(summary = "路径规划-驾车路线", description = "计算两点间的驾车路线")
    @GetMapping("/driving-direction")
    public Result<Map<String, Object>> drivingDirection(
            @Parameter(description = "起点纬度", required = true) @RequestParam double originLat,
            @Parameter(description = "起点经度", required = true) @RequestParam double originLng,
            @Parameter(description = "终点纬度", required = true) @RequestParam double destLat,
            @Parameter(description = "终点经度", required = true) @RequestParam double destLng) {
        Map<String, Object> result = mapService.drivingDirection(originLat, originLng, destLat, destLng);
        if (result.isEmpty()) {
            return Result.failed("路径规划失败");
        }
        return Result.success(result);
    }

    /**
     * 地点检索API，根据关键字检索地点
     *
     * @param keyword 检索关键字
     * @param region 检索区域
     * @return 包含地点检索结果的结果
     */
    @Operation(summary = "地点检索", description = "根据关键字检索地点")
    @GetMapping("/place-search")
    public Result<Map<String, Object>> placeSearch(
            @Parameter(description = "检索关键字", required = true) @RequestParam String keyword,
            @Parameter(description = "检索区域", required = true) @RequestParam String region) {
        Map<String, Object> result = mapService.placeSearch(keyword, region);
        if (result.isEmpty()) {
            return Result.failed("地点检索失败");
        }
        return Result.success(result);
    }

    /**
     * 计算两点间的距离
     *
     * @param originLat 起点纬度
     * @param originLng 起点经度
     * @param destLat 终点纬度
     * @param destLng 终点经度
     * @return 两点间的距离，单位为米
     */
    @Operation(summary = "计算两点间的距离", description = "计算两点间的距离，单位为米")
    @GetMapping("/calculate-distance")
    public Result<Double> calculateDistance(
            @Parameter(description = "起点纬度", required = true) @RequestParam double originLat,
            @Parameter(description = "起点经度", required = true) @RequestParam double originLng,
            @Parameter(description = "终点纬度", required = true) @RequestParam double destLat,
            @Parameter(description = "终点经度", required = true) @RequestParam double destLng) {
        double distance = mapService.calculateDistance(originLat, originLng, destLat, destLng);
        return Result.success(distance);
    }

    /**
     * 计算两点间的驾车时间
     *
     * @param originLat 起点纬度
     * @param originLng 起点经度
     * @param destLat 终点纬度
     * @param destLng 终点经度
     * @return 两点间的驾车时间，单位为秒
     */
    @Operation(summary = "计算两点间的驾车时间", description = "计算两点间的驾车时间，单位为秒")
    @GetMapping("/calculate-driving-time")
    public Result<Integer> calculateDrivingTime(
            @Parameter(description = "起点纬度", required = true) @RequestParam double originLat,
            @Parameter(description = "起点经度", required = true) @RequestParam double originLng,
            @Parameter(description = "终点纬度", required = true) @RequestParam double destLat,
            @Parameter(description = "终点经度", required = true) @RequestParam double destLng) {
        int time = mapService.calculateDrivingTime(originLat, originLng, destLat, destLng);
        return Result.success(time);
    }
} 